package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.TopicDetail;
import com.elinkway.infinitemovies.bean.TopicDetailList;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

public class TopicDetailGridAdapter extends MyBaseAdapter {

	private TopicDetailList topicList;
	
	public TopicDetailGridAdapter(Context context,TopicDetailList topicList) {
		super(context);
		this.topicList = topicList;
	}

	@Override
	public int getCount() {
		return topicList.getTopics().size();
	}


	private class GridItemHolder {
		ImageView poster;
		TextView title;
		TextView intro;
	}

	@Override
	public Object getItem(int position) {
		if(topicList.getTopics() != null){
			return topicList.getTopics().get(position);
		}
		return null;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		TopicDetail detail = topicList.getTopics().get(position);
		GridItemHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.grid_item_poster_small,
					null);
			holder = new GridItemHolder();

			holder.poster = (ImageView) convertView
					.findViewById(R.id.grid_item_poster);
			holder.title = (TextView) convertView
					.findViewById(R.id.grid_item_title);
			holder.intro = (TextView) convertView
					.findViewById(R.id.grid_item_intro);
			convertView.setTag(holder);
		} else {
			holder = (GridItemHolder) convertView.getTag();
		}
		ImageLoader.getInstance().displayImage(detail.getPoster(),
				holder.poster);
		holder.title.setText(detail.getName());
		String episodeInfo = detail.getEpisodeInfoInString();
		holder.intro.setVisibility(View.GONE);
		if(!TextUtils.isEmpty(episodeInfo)){
			holder.intro.setText(episodeInfo);
			holder.intro.setVisibility(View.VISIBLE);
		}else{
			holder.intro.setVisibility(View.GONE);
		}
		return convertView;
	}


	public void addItem_data(TopicDetailList result) {
		// TODO Auto-generated method stub
		topicList.addTopics(result.getTopics());
		notifyDataSetChanged();
	}

	public void setItem_data(TopicDetailList result) {
		// TODO Auto-generated method stub
		topicList = result;
	}
}

